/**
 * @file XFooterView.java
 * @create Mar 31, 2012 9:33:43 PM
 * @author Maxwin
 * @description XListView's footer
 */
package com.aj.activity.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aj.activity.R;

public class XListViewFooter extends RelativeLayout {
	public final static int STATE_NORMAL = 0;
	public final static int STATE_READY = 1;
	public final static int STATE_LOADING = 2;

	private Context mContext;

	private View mContentView;
	private View mProgressBar;
	private TextView mHintView;
	private ImageView xlistview_footer_hint_image;
	private String nomarlTextString = null;

	private boolean isImageVisible = false;

	public XListViewFooter(Context context) {
		super(context);
		initView(context);
	}

	public XListViewFooter(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	public void setState(int state) {
		mHintView.setVisibility(View.INVISIBLE);
		mProgressBar.setVisibility(View.INVISIBLE);
		mHintView.setVisibility(View.INVISIBLE);
		xlistview_footer_hint_image.setVisibility(View.INVISIBLE);
		if (state == STATE_READY) {
			// mSeperatorLine.setVisibility(View.VISIBLE);
			mHintView.setVisibility(View.VISIBLE);
			if (isImageVisible) {
				xlistview_footer_hint_image.setVisibility(View.VISIBLE);
			}
			mHintView.setText(R.string.xlistview_footer_hint_ready);
		} else if (state == STATE_LOADING) {
			// mSeperatorLine.setVisibility(View.VISIBLE);
			mProgressBar.setVisibility(View.VISIBLE);
		} else {
			// RelativeLayout.LayoutParams lp =
			// (RelativeLayout.LayoutParams)mContentView.getLayoutParams();
			// if (lp.height == LayoutParams.WRAP_CONTENT ||
			// mProgressBar.getVisibility() == View.VISIBLE) {
			// mSeperatorLine.setVisibility(View.VISIBLE);
			// } else {
			// mSeperatorLine.setVisibility(View.INVISIBLE);
			// }
			mHintView.setVisibility(View.VISIBLE);
			if (isImageVisible) {
				xlistview_footer_hint_image.setVisibility(View.VISIBLE);
			}
			if (nomarlTextString != null) {
				mHintView.setText(nomarlTextString);
			} else {
				mHintView.setText(R.string.xlistview_footer_hint_normal);
			}
		}
	}

	public void setBottomMargin(int height) {
		if (height < 0)
			return;
		RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mContentView
				.getLayoutParams();
		lp.bottomMargin = height;
		mContentView.setLayoutParams(lp);

		// if (height < 50) {
		// mSeperatorLine.setVisibility(View.INVISIBLE);
		// }
	}

	public int getBottomMargin() {
		RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mContentView
				.getLayoutParams();
		return lp.bottomMargin;
	}

	/**
	 * normal status
	 */
	public void normal() {
		mHintView.setVisibility(View.VISIBLE);
		mProgressBar.setVisibility(View.GONE);
		// mSeperatorLine.setVisibility(View.VISIBLE);
	}

	/**
	 * loading status
	 */
	public void loading() {
		mHintView.setVisibility(View.GONE);
		mProgressBar.setVisibility(View.VISIBLE);
		// mSeperatorLine.setVisibility(View.VISIBLE);
	}

	/**
	 * hide footer when disable pull load more
	 */
	public void hide() {
		RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mContentView
				.getLayoutParams();
		lp.height = 0;
		mContentView.setLayoutParams(lp);

		// setLayoutParams(lp);
	}

	/**
	 * show footer
	 */
	public void show() {
		RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mContentView
				.getLayoutParams();
		lp.height = LayoutParams.WRAP_CONTENT;
		mContentView.setLayoutParams(lp);
		// mSeperatorLine.setVisibility(View.VISIBLE);

		// lp.height = 60;
		// setLayoutParams(lp);
	}

	private void initView(Context context) {
		mContext = context;
		RelativeLayout moreView = (RelativeLayout) LayoutInflater
				.from(mContext).inflate(R.layout.xlistview_footer, null);
		addView(moreView);
		moreView.setLayoutParams(new RelativeLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

		mContentView = moreView.findViewById(R.id.xlistview_footer_content);
		xlistview_footer_hint_image = (ImageView) moreView
				.findViewById(R.id.xlistview_footer_hint_image);
		mProgressBar = moreView.findViewById(R.id.xlistview_footer_progressbar);
		mHintView = (TextView) moreView
				.findViewById(R.id.xlistview_footer_hint_textview);
	}

	public boolean isImageVisible() {
		return isImageVisible;
	}

	public void setImageVisible(boolean isImageVisible) {
		this.isImageVisible = isImageVisible;
	}

	public String getNomarlTextString() {
		return nomarlTextString;
	}

	public void setNomarlTextString(String nomarlTextString) {
		this.nomarlTextString = nomarlTextString;
	}

}
